<template>
    <el-drawer v-model="drawer" :title="title" :size="size" :destroy-on-close="destroyOnClose"
        :close-on-click-modal="false">
        <div class="formDrawer">
            <div class="body">
                <slot></slot>
            </div>
            <div class="actions">
                <el-button type="primary" @click="submit" :loading="loading">{{ confirmText }}</el-button>
                <el-button type="default" @click="close">取消</el-button>
            </div>
        </div>
    </el-drawer>
</template>
<script setup>
import { ref } from 'vue'
const drawer = ref(false)

const props = defineProps({
    title: String,
    size: {
        type: String,
        default: '45%'
    },
    destroyOnClose: {
        type: Boolean,
        default: false
    },
    confirmText: {
        type: String,
        default: '提交'
    }
})

// 按钮预加载
const loading = ref(false)

// 开始预加载
const showLoading = () => loading.value = true

// 结束预加载
const hideLoading = () => loading.value = false

// 打开
const open = () => drawer.value = true

// 关闭
const close = () => drawer.value = false

// 提交
const emit = defineEmits(["submit"])
const submit = () => emit("submit")

// 向父组件暴露方法
defineExpose({
    open,
    close,
    showLoading,
    hideLoading
})
</script>
<style scoped lang="less">
.formDrawer {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;

    .body {
        flex: 1;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 50px;
        overflow-y: auto;
    }

    .actions {
        display: flex;
        align-items: center;
        height: 50px;
        margin-top: auto;
    }
}
</style>